<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <div class="wapperSon">
          <el-autocomplete
            class="inline-input"
            style="max-width:120px;font-size:12px;"
            v-model="queryConditions.sDtlMaterialCode"
            :fetch-suggestions="querySearch"
            placeholder="物料编号"
            @select="handleSelect"
            @input="handleInput"
          ></el-autocomplete>
          <el-input
            v-model="queryConditions.sMaterialName"
            placeholder="产品编号"
            style="max-width:120px;font-size:12px;"
          ></el-input>
          <span>物料类别筛选:</span>
          <el-select
            v-model="queryConditions.sCategoryName"
            placeholder="请选择"
            style="max-width:120px;font-size:12px;"
          >
            <el-option
              v-for="itm in ddCategories"
              :label="itm.label"
              :value="itm.value"
              :key="itm.value"
            ></el-option>
          </el-select></div
      ></el-col>
      <el-col :span="12">
        <el-button-group style="float: right;">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="el-button--medium"
            @click="fetchData"
            >查询</el-button
          >
          <el-button
            type="primary"
            style="float: "
             icon="el-icon-edit"
            @click="goDayReport()"
          >
            物料编辑
          </el-button>
        </el-button-group></el-col
      >
    </el-row>

    <el-row type="flex" justify="start">
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        @row-click="rowClick"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center" label="序号" width="95" v-if="false">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="序号"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="订单号"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.orderNo }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="物料编号"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.sDtlMaterialCode }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="物料名称"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.sMaterialName }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="物料类别"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.sCategoryName }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="颜色"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.sColorName }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="仓库库存"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.nInQty }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="物料提前时间"
          width="120"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.nstockqty }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="规格"
          width="95"
          style="display:none;"
        >
          <template slot-scope="scope">
            {{ scope.row.istockunitid }}
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        :page-sizes="listQuery.pageSizes"
        @pagination="fetchData"
      />
    </el-row>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import request from "@/utils/request";
import {
  getAll,
  list,
  create,
  update,
  deleteBatch,
  getStockMaterials
} from "@/api/wms/WMSStockExApi";
import { getAll as getAllMaterialCatagories } from "@/api/produce/MMMaterialCategoryApi";

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 6,
        pageSizes: [6, 10, 14]
      },
      currentWMSStockExTypeId: 0,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建"
      },
      rules: {
        iid: [
          {
            required: true,
            message: "iid不能为空！",
            trigger: "change"
          }
        ],
        istoreid: [
          {
            required: true,
            message: "istoreid不能为空！",
            trigger: "change"
          }
        ],
        istockexmaterialid: [
          {
            required: true,
            message: "istockexmaterialid不能为空！",
            trigger: "change"
          }
        ],
        ninqty: [
          {
            required: true,
            message: "ninqty不能为空！",
            trigger: "change"
          }
        ],
        iinunitid: [
          {
            required: true,
            message: "iinunitid不能为空！",
            trigger: "change"
          }
        ],
        nunitconvertrate: [
          {
            required: true,
            message: "nunitconvertrate不能为空！",
            trigger: "change"
          }
        ],
        ninstockqty: [
          {
            required: true,
            message: "ninstockqty不能为空！",
            trigger: "change"
          }
        ],
        nstockqty: [
          {
            required: true,
            message: "nstockqty不能为空！",
            trigger: "change"
          }
        ],
        istockunitid: [
          {
            required: true,
            message: "istockunitid不能为空！",
            trigger: "change"
          }
        ],
        ngdnqty: [
          {
            required: true,
            message: "ngdnqty不能为空！",
            trigger: "change"
          }
        ],
        nrtwqty: [
          {
            required: true,
            message: "nrtwqty不能为空！",
            trigger: "change"
          }
        ],
        nrtsqty: [
          {
            required: true,
            message: "nrtsqty不能为空！",
            trigger: "change"
          }
        ],
        nreservedqty: [
          {
            required: true,
            message: "nreservedqty不能为空！",
            trigger: "change"
          }
        ],
        nstockprice: [
          {
            required: true,
            message: "nstockprice不能为空！",
            trigger: "change"
          }
        ],
        nstockamount: [
          {
            required: true,
            message: "nstockamount不能为空！",
            trigger: "change"
          }
        ],
        slocationname: [
          {
            required: true,
            message: "slocationname不能为空！",
            trigger: "change"
          }
        ],
        bpublic: [
          {
            required: true,
            message: "bpublic不能为空！",
            trigger: "change"
          }
        ],
        dpublicdate: [
          {
            required: true,
            message: "dpublicdate不能为空！",
            trigger: "change"
          }
        ],
        spublicuser: [
          {
            required: true,
            message: "spublicuser不能为空！",
            trigger: "change"
          }
        ],
        spublicremark: [
          {
            required: true,
            message: "spublicremark不能为空！",
            trigger: "change"
          }
        ],
        iimstockexmaterialid: [
          {
            required: true,
            message: "iimstockexmaterialid不能为空！",
            trigger: "change"
          }
        ],
        ipounitid: [
          {
            required: true,
            message: "ipounitid不能为空！",
            trigger: "change"
          }
        ],
        ncountqty: [
          {
            required: true,
            message: "ncountqty不能为空！",
            trigger: "change"
          }
        ],
        sremark: [
          {
            required: true,
            message: "sremark不能为空！",
            trigger: "change"
          }
        ]
      },
      temp: {
        id: undefined,
        iid: "",
        istoreid: "",
        istockexmaterialid: "",
        ninqty: "",
        iinunitid: "",
        nunitconvertrate: "",
        ninstockqty: "",
        nstockqty: "",
        istockunitid: "",
        ngdnqty: "",
        nrtwqty: "",
        nrtsqty: "",
        nreservedqty: "",
        nstockprice: "",
        nstockamount: "",
        slocationname: "",
        bpublic: "",
        dpublicdate: "",
        spublicuser: "",
        spublicremark: "",
        iimstockexmaterialid: "",
        ipounitid: "",
        ncountqty: "",
        sremark: ""
      },
      //表格變量
      lstSelected: [],
      queryConditions: {
        sDtlMaterialCode: "",
        sMaterialName: "",
        sCategoryName: ""
      },
      lstOrders: [],
      ddCategories: []
    };
  },
  watch: {},
  computed: {},
  created() {
    this.fetchData();
    this.getAllMaterialCatagories();
  },
  methods: {
    handleSelectionChange(val) {
      this.lstSelected = val;
      const type = this.list.find(v => v.therbligsNo === val.therbligsNo);
      if (type != null) {
        this.currentWMSStockExTypeId = type.id;
      }
    },
    fetchData() {
      let params = {
        page: this.listQuery.page,
        size: this.listQuery.limit
      };
      params = Object.assign(params,this.queryConditions);
      return getStockMaterials(params).then(response => {
        this.list = response.data.records;
        // console.log(this.list);
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    rowClick(row, column, event) {
      this.currentWMSStockExTypeId = row.id;
      this.fetchData();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        iid: "",
        istoreid: "",
        istockexmaterialid: "",
        ninqty: "",
        iinunitid: "",
        nunitconvertrate: "",
        ninstockqty: "",
        nstockqty: "",
        istockunitid: "",
        ngdnqty: "",
        nrtwqty: "",
        nrtsqty: "",
        nreservedqty: "",
        nstockprice: "",
        nstockamount: "",
        slocationname: "",
        bpublic: "",
        dpublicdate: "",
        spublicuser: "",
        spublicremark: "",
        iimstockexmaterialid: "",
        ipounitid: "",
        ncountqty: "",
        sremark: ""
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate() {
      if (this.lstSelected == null || this.lstSelected.length !== 1) {
        this.$message.error("请先选择一条库存记录");
        return;
      }
      this.temp = Object.assign({}, this.lstSelected[0]); // copy obj
      debugger;
      this.temp = { ...this.temp };
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    ui_create() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.temp.id = 0; //自增
          create(this.temp).then(() => {
            this.list.unshift(this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "添加成功！",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },
    ui_update() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          update(this.temp).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id);
            this.list.splice(index, 1, this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "修改成功！",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },
    ui_delete(row, index) {
      if (this.lstSelected == null || this.lstSelected.length === 0) {
        this.$message.error("请先选择要删除的ActionTherbligs");
        return;
      }
      let ids = [];
      this.lstSelected.map(function(elm) {
        ids.push(elm.id);
      });
      deleteBatch(ids).then(() => {
        ids.forEach(elm => {
          const index = this.list.findIndex(v => v.id === elm);
          this.list.splice(index, 1);
        });
        this.dialogFormVisible = false;
        this.$notify({
          title: "Success",
          message: "删除成功！",
          type: "success",
          duration: 2000
        });
      });
    },
    handleMainTypeChange(val) {
      this.temp.wMSStockExTypeId = val;
      this.temp = { ...this.temp };
    },
    querySearch(queryString, cb) {
      let orders = this.lstOrders;
      let results = queryString
        ? this.lstOrders.filter(this.createFilter(queryString))
        : this.lstOrders;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return p => {
        return p.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    handleSelect(val) {
      this.queryConditions.sDtlMaterialCode = val.value;
    },
    handleInput(val) {
      this.queryConditions.sDtlMaterialCode = val;
    },
    getAllMaterialCatagories() {
      getAllMaterialCatagories().then(response => {
        let result = response.data;
        if (result != null && result.length > 0) {
          result.map(v => {
            this.ddCategories.push({ label: v.scategoryname, value: v.scategoryname });
          });
        }
        this.ddCategories.splice(0, 0, { label: "请选择", value: "0" });
      });
    }
  }
};
</script>
<style scoped>
.item1 {
  margin-top: 20px;
}
.wapperSon {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
